package controllers

import (
	"github.com/gin-gonic/gin"
	"my-gin-tiktok/app/daos"
	"my-gin-tiktok/app/services"
	"my-gin-tiktok/common/response"
	"strconv"
)

// Relation 关注/取消关注
func RelationAction(ctx *gin.Context) {
	var form daos.RelationAction
	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, err.Error())
		return
	}

	actionType := form.ActionType
	if actionType != 1 && actionType != 2 {
		response.BusinessFail(ctx, "参数有误")
		return
	}

	var err error
	userID, _ := strconv.ParseInt(ctx.Keys["id"].(string), 10, 64)
	switch actionType {
	case 1:
		err = services.RelationService.CreateRelation(ctx, userID, form.ToUserId)
	case 2:
		err = services.RelationService.DeleteRelation(ctx, userID, form.ToUserId)
	}

	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, nil)
}

// FollowList 我关注的列表
func FollowList(ctx *gin.Context) {
	userID, _ := strconv.ParseInt(ctx.Keys["id"].(string), 10, 64)
	followList, err := services.RelationService.GetFollowList(ctx, userID)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, followList)
}

// FollowList 关注我的列表
func FollowerList(ctx *gin.Context) {
	userID, _ := strconv.ParseInt(ctx.Keys["id"].(string), 10, 64)
	followerList, err := services.RelationService.GetFollowerList(ctx, userID)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, followerList)
}
